package cn.tedu.egmvc4.controller;

import cn.tedu.egmvc4.base.response.JsonResult;
import cn.tedu.egmvc4.mapper.UserMapper;
import cn.tedu.egmvc4.pojo.dto.UpdateUserParam;
import cn.tedu.egmvc4.pojo.dto.UserAddParam;
import cn.tedu.egmvc4.pojo.entity.User;
import cn.tedu.egmvc4.pojo.vo.UserListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/user/")
public class UserController {
    @Autowired
    public UserMapper userMapper;

    /**
     * 添加用户
     * @param userAddParam
     * @return
     */
    @PostMapping("add")
    public JsonResult UserAdd(UserAddParam userAddParam){
        System.out.println("userAddParam:"+userAddParam);
        User user = new User();
        BeanUtils.copyProperties(userAddParam,user);
        user.setCreateTime(new Date());
        userMapper.insertUser(user);

        return JsonResult.success();
    }

    /**
     * 查询用户列表
     * @param userListVO
     * @return
     */
    @GetMapping("list")
    public JsonResult userListVOS(UserListVO userListVO){
        System.out.println("userListVO:"+userListVO);
        List<UserListVO> userListVOS = userMapper.selectUser();

        return JsonResult.success(userListVOS);
    }

    /**
     * 删除指定用户
     * @param id
     * @return
     */
    @PostMapping("del")
    public JsonResult delectUser(Integer id){
        System.out.println("id=" +id);
        userMapper.deleteUser(id);

        return JsonResult.success();
    }

    /**
     * 更新指定用户
     * @param updateUserParam
     * @return
     */
    @PostMapping("update")
    public JsonResult UpdateUser(UpdateUserParam updateUserParam){
        System.out.println("updateUserParam:"+updateUserParam);
        User user = new User();
        userMapper.updateUser(user);

        return JsonResult.success();
    }
}
